package com.sso.service;

import com.yyl.common.pojo.TaotaoResult;
import com.yyl.common.utils.CookieUtils;
import com.yyl.common.utils.JedisClient;
import com.yyl.common.utils.JsonUtils;
import com.yyl.mapper.TbUserMapper;
import com.yyl.pojo.TbUser;
import com.yyl.pojo.TbUserExample;
import com.yyl.pojo.TbUserExample.Criteria;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
/**
 * 用户登录服务
 * @author yangyl
 *
 */
@Service
public class LoginServiceImpl implements LoginService {
	
	@Autowired
	private TbUserMapper userMapper;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${REDIS_SESSION_KEY}")
	private String REDIS_SESSION_KEY;
	
	@Value("${SESSION_EXPIRE}")
	private int SESSION_EXPIRE;
	
	@Override
	public TaotaoResult Login(String uesrname, String password, HttpServletRequest request,
			HttpServletResponse response) {
		// 校验用户名和密码是否正确
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(uesrname);
		List<TbUser> list =  userMapper.selectByExample(example);
		//取用户信息
		if(list.isEmpty() || list == null) {
			return TaotaoResult.build(400, "用户名或密码错误");
		}
		TbUser user = list.get(0);
		//校验密码
		if(!user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
			return TaotaoResult.build(400, "用户名或密码错误");
		}
		//登录成功
		//生成token
		String token = UUID.randomUUID().toString();
		//把用户信息写入redis
		//key:REDIS_SESSION：(token) 
		//value： user转json
		//清空密码
		user.setPassword(null);
		jedisClient.set(REDIS_SESSION_KEY+":"+token, JsonUtils.objectToJson(user));
		//设置session的过期时间
		jedisClient.expire(REDIS_SESSION_KEY+":"+token, SESSION_EXPIRE);
		//写cookie
		CookieUtils.setCookie(request, response, "TT_TOKEN", token);
		return TaotaoResult.ok(token);
	}

	@Override
	public TaotaoResult getUserByToken(String token) {
		// 根据token取用户信息
		String json = jedisClient.get(REDIS_SESSION_KEY+":"+token);
		//判断是否查询到结果
		if(StringUtils.isBlank(json)) {
			return TaotaoResult.build(400, "用户session已经过期");
		}
		//更新过期时间
		jedisClient.expire(REDIS_SESSION_KEY+":"+token, SESSION_EXPIRE);
		//把json转换成对象
		TbUser user = JsonUtils.jsonToPojo(json, TbUser.class);
		return TaotaoResult.ok(user);
	}

	@Override
	public TaotaoResult logout(String token) {
		// TODO Auto-generated method stub
		// 根据token取用户信息
		String json = jedisClient.get(REDIS_SESSION_KEY+":"+token);
		//判断是否查询到结果
		if(StringUtils.isBlank(json)) {
			return TaotaoResult.build(400, "用户session已经过期");
		}
		jedisClient.del(REDIS_SESSION_KEY+":"+token);
		return TaotaoResult.ok();
	}

}
